package com.cloud.wms.ui.main.fragment;

import android.os.Bundle;
import android.os.Message;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.expand.ExpandLayout;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.base.BaseViewPagerFragment;
import com.cloud.wms.ui.main.activity.BaseInfoActivity;
import com.cloud.wms.ui.main.contract.MainContract;
import com.cloud.wms.ui.main.entity.MemberEntity;
import com.cloud.wms.ui.main.entity.ModelEntity;
import com.cloud.wms.ui.main.presnter.MainPresenter;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;
import com.trello.rxlifecycle3.LifecycleTransformer;

import java.util.List;

import butterknife.BindView;

public class ModelFragment extends BaseViewPagerFragment<MainPresenter> implements MainContract.View {
    @BindView(R.id.rv_parent_model)
    RecyclerView rvParentModel;
    private BaseQuickAdapter<ModelEntity, BaseViewHolder> mAdapter;

    @Override
    protected int getResId() {
        return R.layout.fragment_main_model;
    }

    @Override
    protected void setupComponent() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initEvent(Bundle bundle) {
        rvParentModel.setLayoutManager(new LinearLayoutManager(this.getContext()));
        mAdapter = new BaseQuickAdapter<ModelEntity, BaseViewHolder>(R.layout.item_main_model) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, ModelEntity item) {
                helper.setText(R.id.tv_main_title, item.getModuleName());
                ImageView ivArrow = helper.getView(R.id.iv_main_arrow);
                ExpandLayout expand = helper.getView(R.id.expand_layout);
                expand.setOnExpansionUpdateListener((expansionFraction, state) -> {
                    ivArrow.setRotation(expansionFraction * 180);
                });
                helper.getView(R.id.ll_title_content).setOnClickListener(v -> expand.toggle(true));

                RecyclerView rvModel = helper.getView(R.id.rv_model);
                rvModel.setLayoutManager(new GridLayoutManager(ModelFragment.this.getContext(), 3));
                List<ModelEntity> childModelList = Stream.ofNullable(item.getChildModel()).sortBy(ModelEntity::getSortNum).toList();
                BaseQuickAdapter mAdapter = new BaseQuickAdapter<ModelEntity, BaseViewHolder>(R.layout.item_main_child_model, childModelList) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder helper, ModelEntity item) {
                        helper.setText(R.id.tv_model_name, item.getModuleName())
                                .setImageResource(R.id.iv_model_icon, item.getIcon());
                    }
                };
                mAdapter.setOnItemClickListener((adapter, view, position) -> {
                    MemberEntity memberInfo = FineExApplication.component().loginCache().getMemberInfo();
                    if (memberInfo == null) {
                        new AlertDialogBuilder(getContext())
                                .setTitle("选择会员")
                                .setMessage("请选择会员后操作")
                                .setPositiveButton("确定", (dialog, which) -> switchMember())
                                .setNegativeButton("取消", null)
                                .show();
                        return;
                    }
                    mPresenter.matchPath(((ModelEntity) adapter.getItem(position)));
                });
                rvModel.setAdapter(mAdapter);
            }
        };

        rvParentModel.setAdapter(mAdapter);
        mAdapter.setNewInstance(FineExApplication.component().loginCache().getModelList());
    }

    private void switchMember() {
        BaseInfoActivity.start(getContext(), true);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(Message message) {

    }

    @Override
    public <R> LifecycleTransformer<R> toLifecycle() {
        return bindToLifecycle();
    }
}
